﻿using Abp.Application.Services.Dto;
using Abp.Application.Services;
using Choucius.Boilerplate9.InboundRubberFrameLogs.Dto;
using Abp.Domain.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;

namespace Choucius.Boilerplate9.InboundRubberFrameLogs
{
    /// <summary>
    /// 胶框入库
    /// </summary>
    public class InboundRubberFrameLogAppService : AsyncCrudAppService<InboundRubberFrameLog, InboundRubberFrameLogDto, long, PagedResultRequestDto, CreateInboundRubberFrameLogDto, CreateInboundRubberFrameLogDto>, IInboundRubberFrameLogAppService
    {
        private readonly IRepository<InboundRubberFrameLog, long> _logRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public InboundRubberFrameLogAppService(IRepository<InboundRubberFrameLog, long> repository) : base(repository)
        {
            _logRepository = repository;
        }

        /// <summary>
        /// 入库统计
        /// </summary>
        /// <returns></returns>
        public async Task<List<InboundRubberFrameStatisticDto>> GetInboundStatistics()
        {
            var entities = await _logRepository.GetAllListAsync();
            var result = entities
                .GroupBy(x => x.CreationTime.Date) // 分组，按日分组
                .Select(group => new InboundRubberFrameStatisticDto
                {
                    TimeInterval = $"{group.Key.Date.ToShortDateString()}",
                    TotalCount = group.Sum(x => x.LabelCount)
                })
                .ToList();
            return result;
        }
    }
}
